#!/usr/bin/env python
#coding=utf-8
import Globals
import Borad
import Spider
import Casing
class Observable:
    def __init__(self, initialValue=None):
        self.data = initialValue
        self.callbacks = {}
        self.intFlag = 0

    def addCallback(self, func):
        self.intFlag = self.intFlag+1
        self.callbacks[func] = self.intFlag

    def delCallback(self, func):
        del self.callback[func]

    def _docallbacks(self):
        for func in self.callbacks:
            func(self.data)
    def set(self, data):
        self.data = data
        self._docallbacks()

    def get(self):
        return self.data

    def unset(self):
        self.data = None
class Model:
    def __init__(self):
        self.observable = Observable(0)
        self.boradObject = Borad.Borad("http://www.cndev.org/main/cu_index.aspx");
        self.boradData = self.boradObject.getDBborad();       
    
    def SetBoradNamePage(self,boradName,page):
        #def f():
        self.observable.set(self.GetRightTreeData(self.GetBoradIDByName(boradName),page))
        #d = Casing.Casing(f)
        #d.start_thread()        
    def GetCurrentData(self):
        return self.observable.get()
    def GetBoradIDByName(self,name):
        boradID = ''
        for k,v in self.boradData:
            if v == name:
                boradID = k
        return boradID
    def GetRightTreeData(self,borad,page):
        return self.boradObject.GetRootTreeNodes(borad,page)
    def GetBoradTexts(self):
        lstBoradtext = []
        for i in self.boradData:
            lstBoradtext.append(i[1])
        return lstBoradtext
    def GetNewBoradData(self):
        self.boradObject.SyncAllBorad()
    def GetNewMessages(self,callback):
        sp  = Spider.Spider(callback)
        return sp.InitDataFromCndev()
        
